<?php


namespace app\api\controller;


use app\api\model\Collage\CollageModel;
use app\api\model\Order\OrderGoodsModel;
use app\api\model\Order\OrderModel;
use app\api\model\User\MerchantWalletModel;
use app\api\model\User\UserWalletModel;
use app\Utils\StrUtil;
use app\Utils\Util;
use think\facade\Db;

class Task extends AppController
{
    /**
     * 定时退款关闭和退款
     * 超过1天未付款的订单自动关闭
     * 超过15天已发货的订单自动确认收货完成
     * 每次处理10个订单
     */
    public function taskOrderCancel()
    {
        $where = [];
        $where_1 = [
            ['status','=',0],
            ['add_time','<',date("Y-m-d H:i:s",(time()-86400))]
        ];
        array_push($where,$where_1);
        $where_2 = [
            ['status','=',2],
            ['up_time','<',date("Y-m-d H:i:s",(time()-86400*15))]
        ];
        array_push($where,$where_2);

        $list = OrderModel::getOrderListWhereOrPage(1,10,$where);
        foreach ($list as $item){
            $order_save = [
                'up_time' => date("Y-m-d H:i:s",time())
            ];
            if($item['status'] == 0){
                $order_save['status'] = -1;
            }
            if($item['status'] == 2){
                $order_save['status'] = 3;
            }
            $flag = OrderModel::updateOrderWhere([['id','=',$item['id']]],$order_save);
            if($flag){
                $order_goods = OrderGoodsModel::getOrderGoodsListWhereMore([['order_id','=',$item['id']]]);
                foreach ($order_goods as $val){
                    $goods_save = [
                        'up_time' => date("Y-m-d H:i:s",time())
                    ];
                    if($item['status'] == 0){
                        $goods_save['status'] = -1;
                    }
                    if($item['status'] == 2){
                        $goods_save['status'] = 3;
                    }
                    $flag = OrderGoodsModel::updateOrderGoodsWhere([['id','=',$val['id']]],$goods_save);
                    if($flag){
                        if($item['status'] == 2 && $item['type'] == 1){
                            ## 平台抽成
                            if ($item['mer_id']) {
                                $item['ratio'] = $item['ratio'] > 1 ? 1 : $item['ratio'];
                                #增加商家钱包余额
                                MerchantWalletModel::walletIn($item['mer_id'], round($item['total_price'] * (1 - $item['ratio']), 2), StrUtil::MER_BILL_USE_TYPE['SELL_GOODS'], '售卖商品');
                            }
                            ## 发放佣金
                            if ($item['is_sales'] == 1 || $item['is_sales'] == 2) {
                                if (!empty($item['agnet_uid1']) && $item['agent_amount1'] > 0 && $item['agent_status1'] == 0) {
                                    UserWalletModel::walletIn($item['agnet_uid1'], $item['agent_amount1'], StrUtil::BILL_USE_TYPE['GOODS_COMMISSION'], "商品佣金", $item['id']);
                                    OrderGoodsModel::updateOrderGoodsWhere(['id' => $item['id']], ['agent_status1' => 1]);
                                    Db::name('user_wallet')->where('uid', $item['agnet_uid1'])->inc('reward', $item['agent_amount1'])->update();
                                }
                                if (!empty($item['agnet_uid2']) && $item['agent_amount2'] > 0 && $item['agent_status2'] == 0) {
                                    UserWalletModel::walletIn($item['agnet_uid2'], $item['agent_amount2'], StrUtil::BILL_USE_TYPE['GOODS_COMMISSION'], "商品佣金", $item['id']);
                                    OrderGoodsModel::updateOrderGoodsWhere(['id' => $item['id']], ['agent_status2' => 1]);
                                    Db::name('user_wallet')->where('uid', $item['agnet_uid2'])->inc('reward', $item['agent_amount2'])->update();
                                }
                                if (!empty($item['agnet_uid3']) && $item['agent_amount3'] > 0 && $item['agent_status3'] == 0) {
                                    UserWalletModel::walletIn($item['agnet_uid3'], $item['agent_amount3'], StrUtil::BILL_USE_TYPE['GOODS_COMMISSION'], "商品佣金", $item['id']);
                                    OrderGoodsModel::updateOrderGoodsWhere(['id' => $item['id']], ['agent_status3' => 1]);
                                    Db::name('user_wallet')->where('uid', $item['agnet_uid3'])->inc('reward', $item['agent_amount3'])->update();
                                }
                            }
                        }
                    }
                }
            }

        }
        echo "执行完成";
        exit();
    }

    /**
     * 定时退款团购订单
     * 超过1天未完成拼团退款，及关闭订单
     */
    public function taskGroupOrderCancel()
    {
        $where = [
            ['c.status','=',0],
            ['c.add_time','<',date("Y-m-d H:i:s",(time()-86400))]
        ];
        $collage_list = CollageModel::getCollageListWhereMore($where);
        foreach ($collage_list as $collage){
            $order_list = OrderModel::getOrderListWhereMore([['collage_id','=',$collage['id']],['status','=',1]]);
            foreach ($order_list as $order){
                //订单退款
                if($order['pay_way'] == 2){
                    //支付宝
                    $result = $this->aliRefund($order['total_price'],$order['out_trade_no']);
                }
                if($order['pay_way'] == 3){
                    //微信
                }
                if ($result['code'] == 0) {
                    $save = [
                        'status' => -1,
                        'up_time' => date("Y-m-d H:i:s",time())
                    ];
                    OrderModel::updateOrderWhere([['id','=',$order['id']]],$save);
                    $order_goods = OrderGoodsModel::getOrderGoodsListWhereMore([['order_id','=',$order['id']]]);
                    foreach ($order_goods as $item){
                        $goods_save = [
                            'status'  => -1,
                            'up_time' => date("Y-m-d H:i:s",time())
                        ];
                        OrderGoodsModel::updateOrderGoodsWhere([['id','=',$item['id']]],$goods_save);
                    }
                }
            }
            $collage_save = [
                'status'  => -1,
                'up_time' => date("Y-m-d H:i:s",time())
            ];
            CollageModel::updateCollageData([['id','=',$collage['id']]],$collage_save);
        }
        echo "执行完成";
        exit();
    }

}
